package yx.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	
	public static String getSystemDate(String pattern){
		SimpleDateFormat sdf = null;
		if(pattern == null || "".equals(pattern)){
			sdf = new SimpleDateFormat("yyyy-MM-dd");
		} else {
			sdf = new SimpleDateFormat(pattern);
		}
		return sdf.format(Calendar.getInstance().getTime());
	}

	public static String getSystemDate(){
		return getSystemDate(null);
	}
	
	public static String getSystemDateTime(String pattern){
		SimpleDateFormat sdf = null;
		if (pattern == null|| "".equals(pattern)){
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		} else {
			sdf = new SimpleDateFormat(pattern);
		}
		return sdf.format(Calendar.getInstance().getTime());
	}
	
	public static String getSystemDateTime(){
		return getSystemDateTime(null);
	}
	
	public static String getSystemDate(Date indate, String pattern){
		SimpleDateFormat sdf = null;
		if (pattern == null || "".equals(pattern)){
			sdf = new SimpleDateFormat("yyyy-MM-dd");
		} else {
			if(indate == null){
				return null;
			} else {
				sdf = new SimpleDateFormat(pattern);
			}
		}
		return sdf.format(indate);
	}
	
	public static Date stringToDate(String indate, String pattern) {
		SimpleDateFormat sdf = null;
		Date retdate = null;
		try {
			if (pattern == null || "".equals(pattern)) {
				sdf = new SimpleDateFormat("yyyy-MM-dd");
			} else {
				sdf = new SimpleDateFormat(pattern);
			}
			retdate = sdf.parse(indate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retdate;
	}
	
	public static Date formateToDate(Date indate, String pattern) {
		SimpleDateFormat sdf = null;
		Date retdate = null;
		try {
			if (pattern == null || "".equals(pattern)) {
				sdf = new SimpleDateFormat("yyyy-MM-dd");
			} else {
				sdf = new SimpleDateFormat(pattern);
			}
			String strDate = sdf.format(indate);
			retdate = sdf.parse(strDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retdate;
	}
	
	public static String formateDateToString(Date indate, String pattern) {
		SimpleDateFormat sdf = null;
		String retdate = null;
		try {
			if (pattern == null || "".equals(pattern)) {
				sdf = new SimpleDateFormat("yyyy-MM-dd");
			} else {
				sdf = new SimpleDateFormat(pattern);
			}
			retdate = sdf.format(indate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retdate;
	}
	
	public static void main(String[] args){
		System.out.println(Calendar.getInstance().get(Calendar.HOUR));
		System.out.println(DateUtils.getSystemDate());
		Date date = stringToDate("2099-12-31", null);
		System.out.println(date);
	}
}
